package com.gz.seckill.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.junit.jupiter.api.Test;

public class MD5Util {

    public static String md5(String str) {
        return DigestUtils.md5Hex(str);
    }

    //准备一个盐salt(随机8位)
    private static final String SALT="rPT336Bp";

    //第一次加密加盐，完成的任务就是md5(password明文+salt1)
    public static String inputPassToMidPass(String inputPass) {
        String str = SALT.charAt(0)+inputPass+SALT.charAt(6);
        return md5(str);
    }

    //第二次加密加盐，完成的任务就是md5(md5(password明文+salt1)+salt2)
    public static String MidPassToDBPass(String midPass,String salt) {
        String str = salt.charAt(1)+midPass+salt.charAt(5);
        return md5(str);
    }

    //编写一个方法将password明文直接转成DB中的密码
    public static String inputPassToDBPass(String inputPass,String salt) {
        String midPass = inputPassToMidPass(inputPass);
        String dbPass = MidPassToDBPass(midPass, salt);
        return dbPass;
    }

    @Test
    public void f1(){
//        String password = inputPassToDBPass("123456", "EmuFcKc3");
        String password = inputPassToDBPass("123456", "RyMC8Nt5");
        System.out.println(password);
    }

    @Test
    public void f2(){
        String midPass = inputPassToMidPass("2028ad83f1997056c7d60e16c36d10a7");
        System.out.println(midPass);
    }
}
